package live.tag;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;


public class HotelPageTag extends TagSupport {

	private Object pages;
	private Object ptotal;
	private Object formUrl;
	private int page;
	private int total;
	
	public Object getPages() {
		return pages;
	}

	public void setPages(Object pages) throws JspException {
		this.pages = ExpressionEvaluatorManager.evaluate("pages", pages.toString(),Object.class, this, pageContext);
		if(pages!=null){
			page = strToInteger(pages);
		}
	}


	public Object getPtotal() {
		return ptotal;
	}

	public void setPtotal(Object ptotal) throws JspException {
		this.ptotal = ExpressionEvaluatorManager.evaluate("ptotal", ptotal.toString(), Object.class,this,pageContext);
		if(ptotal!=null){
			total = strToInteger(ptotal);
		}
	}
	
	public Object getFormUrl() {
		return formUrl;
	}

	public void setFormUrl(Object formUrl) {
		this.formUrl = formUrl;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}




	private static final long serialVersionUID = -4371771906432281168L;

	@Override
	public int doEndTag() throws JspException {
		// TODO Auto-generated method stub
		
		StringBuffer pageBuf = new StringBuffer("<div class=\"paging\">")
		.append("<a href=\"javascript:void(0)\" title=\"首页\"");
		int count=0;
		if(page%5!=0){
			count=page/5;
		}else{
			count = page/5-1;
		}
		if(page==1){
			pageBuf.append(" onclick=\"javascript:alert(&#39;已是第一页&#39;)\"");
		}else{
			pageBuf.append(" onclick=\"").append("toPage(1);return false;\"");
			//pageBuf.append(" onclick=\"javascript:document.forms[0].pages.value=1;document.forms[0].submit();return false;\"");
		}
		pageBuf.append(">&lt;&lt;</a>\n");
		pageBuf.append("<a href=\"javascript:void(0)\" title=\"上一页\"");
		if(page==1){
			pageBuf.append(" onclick=\"javascript:alert(&#39;已是第一页&#39;)\"");
		}else{
			pageBuf.append(" onclick=\"").append("toPage(").append(page-1).append(");return false;\"");
		}
		pageBuf.append(">&lt;</a>\n");
		if(page>5){
			pageBuf.append(" <a href=\"javascript:void(0)\" onclick=\"")
			//.append("document.form1.pages.value="+(count*5)+";document.form1.submit();return false;\">...</a>");
			.append("toPage(").append(count*5).append(");return false;\">...</a>");
		}
		for (int i = (count*5)+1; i <= (count+1)*5; i++) {
			if(i<=total){
			pageBuf.append("<a");
			if(i==page){
				pageBuf.append(" style=\"border-right: #000099 1px solid; padding-right: 5px; border-top: #000099 1px solid; padding-left: 5px; font-weight: bold; padding-bottom: 2px; margin: 2px; border-left: #000099 1px solid; color: #fff; padding-top: 2px; border-bottom: #000099 1px solid; background-color: #000099\"");
			}
				pageBuf.append(" href=\"javascript:void(0)\" title=\""+i+"页\" onclick=\"").append("toPage(").append(i).append(");return false;\">"+i+"</a>");
			}
		}
		if(total-((count+1)*5)>0){
			pageBuf.append(" <a href=\"javascript:void(0)\"")
			.append("toPage(").append((count+1)*5+1).append(");return false;\">...</a>");
			//.append("onclick=\"document.form1.pages.value="+((count+1)*5+1)+";document.form1.submit();return false;\">...</a>");
			
		}
		
		pageBuf.append("<a href=\"javascript:void(0)\" title=\"下一页\" onclick=\""); 
		if(page<total){
			pageBuf.append("toPage(").append(page+1).append(");return false;\"");
		}else{
			pageBuf.append("javascript:alert(&#39;已是最后页&#39;)\"");
		}
		pageBuf.append(">&gt;</a>")
		.append("<a href=\"javascript:void(0)\" title=\"末页/共"+total+"页\" onclick=\"");
		if(page==total){
			pageBuf.append("javascript:alert(&#39;已是最后页&#39;)\"");
		}else{
			pageBuf.append("toPage(").append(total).append(");return false;\"");
		}
		pageBuf.append(">&gt;&gt;</a>").append("</div>");
		pageBuf.append("<script>function toPage(pageno){document.form1.pages.value=pageno;document.forms(0).submit();}</script>");
		try {
			pageContext.getOut().write(pageBuf.toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return super.doEndTag();
	}
	
	private int strToInteger(Object obj){
		return Integer.parseInt(obj.toString());
	}
	
	public static void main(String[] args) {
		int page=6;
		int num=5;
		if(page%5==0){
			
		}
		for (int i = page/num*5+1; i <=page/num+num; i++) {
			System.out.println(i);
		}
	}
}
